/*************************************************************************
	> File Name: TcpService.c
	> Author: 
	> Mail: 
	> Created Time: Mon 25 Jun 2018 10:09:39 AM CST
 ************************************************************************/

#include<stdio.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<strings.h>
#include<arpa/inet.h>
#include <sys/time.h>
#include <unistd.h>
#include "uart_operation.h"
#include "common.h"
#include "sqlite3_api.h"
#include <time.h>
#include<pthread.h>
#include "net.h"


ENV env;
CARD card;
sqlite3 *pDb;
char UartBuf[UART_BUF_SIZE]={0};
int zgbfd;

pthread_mutex_t mutex1;
pthread_mutex_t mutex2;
pthread_mutex_t mutex3;


void *threadFUN1(void *arg)
{
	int connfd = *((int*)arg);
	while(1)
	{
		sleep(1);
		pthread_mutex_lock(&mutex1);
		if(do_zgb(zgbfd, pDb, UartBuf, UART_BUF_SIZE, &env, &card) < 0)		//
		{
			continue;
		}
		do_sendEnv(connfd, pDb, &env,&card);											//send Tem & Hum to client
		pthread_mutex_lock(&mutex1);
	}
}


void *threadFUN2(void *arg)
{
	//int udpSfd = *((int*)arg);
	int cfd  = *((int *)arg);
	while(1)
	{
		sleep(1);
		pthread_mutex_lock(&mutex2);
		/*
		char buffer[1024] = {0};
		int ret;
		struct sockaddr_in seraddr;
		memset(&seraddr,0,sizeof(seraddr));
		seraddr.sin_family = AF_INET;
		seraddr.sin_port = htons(8888);
		seraddr.sin_addr.s_addr = inet_addr("192.168.1.109");
		*/
		
		while(1)
		{
			sleep(1);
			//if(do_camera(cfd, &seraddr) != 0)											//处理视频相关
			if(do_camera1(cfd) != 0)
			{
				printf("do camera error\n");						//send pictures to client
			}
		}   
		close(cfd);
		pthread_mutex_unlock(&mutex2);
	}
}

void *threadFUN3(void *arg)
{
	int cfd = *((int*)arg);
	while(1)
	{
		pthread_mutex_lock(&mutex3);													
		do_client(cfd , pDb);
		pthread_mutex_unlock(&mutex3);
	}
}


int main()
{
    char **presult;
    char Sq[SQLCMD_SIZE] = {0};//存放时间的buf;
	int servfd, udpSfd;
	
	//数据库初始化
	if(Init_DB(&pDb, "my.db") < 0)
	{
		printf("init error\n");
	}
	printf("Db init ok\n");
	
	//串口初始化
	zgbfd = UART0_Open("/dev/ttyUSB0");
	if (zgbfd == -1)
	{
		sleep(1);
        printf("UART0_Open");
	}
	else
	{
		fprintf(stdout, "init zigbee success\n");
	}		
	init_tty(zgbfd);
	printf("init success\n");
	
	//网络初始化
	if (net_init(&servfd) < 0)
	{
		printf("net init error\n");
		return -1;
	}
	/*
	if(udp_init(&udpSfd) < 0)
	{
		printf("udp init error\n");
		return -1;
	}
	*/
	printf("net init ok\n");
	printf("listening.......\n");
	
	pthread_mutex_init(&mutex1,NULL);	
	pthread_mutex_init(&mutex2,NULL);
	pthread_mutex_init(&mutex3,NULL);
	
    while(1)
	{
		int cfd = accept(servfd,NULL,NULL);			
		if(cfd<0)
		{
			sleep(1);
			perror("accept faile:");
			continue;
		}
		printf("accept ok\n");
		
		pthread_t th1;
		//pthread_t th2;
		pthread_t th3;
		
		while(pthread_create(&th1,NULL,threadFUN1,&cfd)<0);		//	do_zgb
		//while(pthread_create(&th2,NULL,threadFUN2,&cfd)<0);		//	do_camera
		while(pthread_create(&th3,NULL,threadFUN3,&cfd)<0);		//	do_client
		
		pthread_detach(th1);
	//	pthread_detach(th2);
		pthread_detach(th3);
	}
	
    close(servfd);
    return 0;
}



